package com.example.cinema.drawview;

import android.animation.Animator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.cinema.R;

import java.util.Timer;
import java.util.TimerTask;
//底部导航
public class BtmNavBar extends LinearLayout {



    public BtmNavBar(Context context) {
        super(context);
    }

    public BtmNavBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    private int seleColor;
    private int hintColor;
    public BtmNavBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }





    private boolean istouch = true;
    private int index = 0;
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int event = ev.getActionMasked();

        int len = getChildCount();
        float x = ev.getX();
        int width = getMeasuredWidth();
        int itemwidth = width / len;
        View view = null;
        if(event == MotionEvent.ACTION_DOWN) {
            for (int i = 0; i < len; i++) {
                if (x > (itemwidth * i) && x < (i + 1) * itemwidth) {
                    index = i;
                    view = getChildAt(index);
                }
                if (i != 2) {
                    TextView tv = (TextView) getChildAt(i);
                    tv.setTextColor(Color.parseColor("#8E8E8E"));
                }
            }
        }
        switch (event)
        {
            case MotionEvent.ACTION_DOWN:
                if(istouch) {
                    istouch = false;
                    if(index!=2)
                    {
                        TextView tv = (TextView) view;
                        if(tv!=null)
                        {
                            tv.setTextColor(Color.parseColor("#ffffff"));
                        }

                    }
                    Enlarge(view);
                    if(itemlistener!=null)
                    {
                        itemlistener.ItemClick(view,index);
                    }
                }
                break;
        }

        return true;
    }

    private void Enlarge(View view) {
        view.animate().scaleXBy(0.3f).scaleYBy(0.3f).setInterpolator(new BounceInterpolator()).withEndAction(new Runnable() {
            @Override
            public void run() {
                Narrow(view);
            }
        }).setDuration(100).start();

    }
    private void Narrow(View view) {
        view.animate().scaleXBy(-0.3f).scaleYBy(-0.3f).setInterpolator(new BounceInterpolator()).setDuration(100).withEndAction(new Runnable() {
            @Override
            public void run() {
                istouch = true;
            }
        }).start();
    }

    //外调接口
    public interface OnItemClickListener{
        void ItemClick(View view,int index);
    }
    private OnItemClickListener itemlistener;
    public void setOnItemClickListener(OnItemClickListener itemlistener)
    {
        this.itemlistener = itemlistener;
    }

}
